
package com.jf.cloud.flow.controller.multishop;

import com.jf.cloud.api.product.dto.ProdEffectDTO;
import com.jf.cloud.api.product.vo.ProdEffectRespVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.flow.dto.ProductAnalyseDTO;
import com.jf.cloud.flow.model.ProductAnalyse;
import com.jf.cloud.flow.service.FlowService;
import com.jf.cloud.flow.service.ProductAnalyseService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 流量分析—商品分析
 *
 * @author zz
 * @date 2021-05-21 15:25:19
 */
@RestController("multishopProductAnalyseController")
@RequestMapping("/m/product_analyse")
@Tag(name = "流量分析—商品分析")
public class ProductAnalyseController {

    @Autowired
    private ProductAnalyseService productAnalyseService;

    @Autowired
    private FlowService flowService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取流量分析—商品分析列表" , description = "分页获取流量分析—商品分析列表")
    public ServerResponseEntity<PageVO<ProductAnalyse>> page(@Valid PageDTO pageDTO) {
        PageVO<ProductAnalyse> productAnalysePage = productAnalyseService.page(pageDTO);
        return ServerResponseEntity.success(productAnalysePage);
    }

    @GetMapping
    @Operation(summary = "获取流量分析—商品分析" , description = "根据productAnalyseId获取流量分析—商品分析")
    public ServerResponseEntity<ProductAnalyse> getByProductAnalyseId(@RequestParam Long productAnalyseId) {
        return ServerResponseEntity.success(productAnalyseService.getByProductAnalyseId(productAnalyseId));
    }

    @PostMapping
    @Operation(summary = "保存流量分析—商品分析" , description = "保存流量分析—商品分析")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ProductAnalyseDTO productAnalyseDTO) {
        ProductAnalyse productAnalyse = mapperFacade.map(productAnalyseDTO, ProductAnalyse.class);
        productAnalyse.setProductAnalyseId(null);
        productAnalyseService.save(productAnalyse);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新流量分析—商品分析" , description = "更新流量分析—商品分析")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ProductAnalyseDTO productAnalyseDTO) {
        ProductAnalyse productAnalyse = mapperFacade.map(productAnalyseDTO, ProductAnalyse.class);
        productAnalyseService.update(productAnalyse);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除流量分析—商品分析" , description = "根据流量分析—商品分析id删除流量分析—商品分析")
    public ServerResponseEntity<Void> delete(@RequestParam Long productAnalyseId) {
        productAnalyseService.deleteById(productAnalyseId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/get_product_effect")
    @Operation(summary = "获取商家端商品洞察数据" , description = "获取商品洞察数据")
    public ServerResponseEntity<PageVO<ProdEffectRespVO>> getProductEffect(@Valid PageDTO pageDTO,@ParameterObject ProdEffectDTO prodEffectDTO){
        prodEffectDTO.setShopId(AuthUserContext.get().getTenantId());
        prodEffectDTO.setLang(I18nMessage.getLang());
        flowService.statisticalProduct();
        return ServerResponseEntity.success(productAnalyseService.getProductEffect(pageDTO,prodEffectDTO));
    }
}
